package com.example.chongqingichms.serviceImpl;

import com.example.chongqingichms.Utils.SoulPage;
import com.example.chongqingichms.bean.IchBean;
import com.example.chongqingichms.mapper.IchMapper;
import com.example.chongqingichms.mapper.impl.BaseDaoImpl;
import com.example.chongqingichms.service.IchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IchServiceImpl extends BaseDaoImpl<Integer,IchBean> implements IchService{

    @Autowired
    private IchMapper ichMapper;


    @Override
    public List<IchBean> getAll() {
        return ichMapper.findAll();
    }

    @Override
    public Object dataGrid(SoulPage<IchBean> soulPage) {
        // 拼接自己的业务逻辑
        StringBuilder sql = new StringBuilder();
        sql.append("select * from tb_ich where 1=1");

        // 获取表单条件（自己定义的 form 表单数据，没有可以不写）
        IchBean ichBean = soulPage.getObj();
        Map<String, Object> param = new HashMap<>();
        if (ichBean != null) {
            if (ichBean.getId() !=null ) {
                sql.append(" and id like :id");
                param.put("id", "%" + ichBean.getId() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getName())) {
                sql.append(" and name like :name");
                param.put("name", "%" + ichBean.getName() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getCategory())) {
                sql.append(" and category like :category");
                param.put("category", "%" + ichBean.getCategory() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getNation())) {
                sql.append(" and nation like :nation");
                param.put("nation", "%" + ichBean.getNation() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getLocation())) {
                sql.append(" and location like :location");
                param.put("location", "%" + ichBean.getLocation() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getInheritors())) {
                sql.append(" and inheritors like :inheritors");
                param.put("inheritors", "%" + ichBean.getInheritors() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getLevel())) {
                sql.append(" and level like :level");
                param.put("level", "%" + ichBean.getLevel() + "%");
            }
            if (StringUtils.isNotBlank(ichBean.getAppliances())) {
                sql.append(" and appliances like :appliances");
                param.put("appliances", "%" + ichBean.getAppliances() + "%");
            }

        }
        // 拼接完成自己的业务sql，调用工具方法返回
        return ichMapper.soulDataGrid(soulPage, sql.toString(), param);
    }

    @Override
    public void delete(Integer id) {
        ichMapper.deleteById(id);
    }

    @Override
    public IchBean save(IchBean ichBean) {
        return ichMapper.save(ichBean);
    }

    @Override
    public IchBean findById(Integer id) {
        return ichMapper.findById(id).orElse(null);
    }
}
